﻿using System;
using System.Windows.Input;

namespace Spammer.ViewModels
{
    public class AppViewModels : BaseViewModel
    {
        private LoginViewModel loginVM;
        private RegisterViewModel registerVm;
        private HistoryViewModel historyVM;
        private NewEmailViewModel newEmailVm;
        private ChartViewModel chartVm;

        public BaseViewModel CurrentViewModel { get; set; }

        public ICommand NavigateToLogin { get; set; }

        public ICommand NavigateToRegister { get; set; }

        public ICommand NavigateToHistory { get; set; }

        public ICommand NavigateToNewEmail { get; set; }

        public ICommand NavigateToChart { get; set; }
        
        public AppViewModels()
        {
            this.loginVM = new LoginViewModel();
            this.loginVM.NavigateToHistory += this.NavigateToHistoryEvent;

            this.registerVm = new RegisterViewModel();
            this.registerVm.NavigateToHistory += this.NavigateToHistoryEvent;

            this.historyVM = new HistoryViewModel();
            this.newEmailVm = new NewEmailViewModel();
            this.chartVm = new ChartViewModel();

            this.CurrentViewModel = this.loginVM;

            this.NavigateToLogin = new RelayCommand(this.NavigateToLoginCommand);
            this.NavigateToRegister = new RelayCommand(this.NavigateToRegisterCommand);
            this.NavigateToHistory = new RelayCommand(this.NavigateToHistoryCommand);
            this.NavigateToNewEmail = new RelayCommand(this.NavigateToNewEmailCommand);
            this.NavigateToChart = new RelayCommand(this.NavigateToChartCommand);
        }

        public void NavigateToHistoryEvent(object sender, GoToHistory e)
        {
            this.historyVM.LoadHistory();
            this.CurrentViewModel = this.historyVM;
            this.OnPropertyChanged("CurrentViewModel");
        }

        public void NavigateToLoginCommand(object obj)
        {
            this.CurrentViewModel = this.loginVM;
            this.OnPropertyChanged("CurrentViewModel");
        }

        public void NavigateToRegisterCommand(object obj)
        {
            this.CurrentViewModel = this.registerVm;
            this.OnPropertyChanged("CurrentViewModel");
        }

        public void NavigateToNewEmailCommand(object obj)
        {
            this.CurrentViewModel = this.newEmailVm;
            this.OnPropertyChanged("CurrentViewModel");
        }

        public void NavigateToChartCommand(object obj)
        {
            this.chartVm.LoadChartHistory();
            this.CurrentViewModel = this.chartVm;
            this.OnPropertyChanged("CurrentViewModel");
        }

        public void NavigateToHistoryCommand(object obj)
        {
            this.historyVM.LoadHistory();
            this.CurrentViewModel = this.historyVM;
            this.OnPropertyChanged("CurrentViewModel");
        }
    }
}
